package com.zhuiYun.project.api.dynamicComment.controller;

import com.zhuiYun.project.annotation.OptLog;
import com.zhuiYun.project.api.dynamicComment.entity.Comment;
import com.zhuiYun.project.api.dynamicComment.service.CommentService;
import com.zhuiYun.project.common.response.CommonConstants;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import com.zhuiYun.project.util.pageUtil.PageResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName CommentController
 * @Description 评论controller
 * @Author WJ
 * @Date 2023/4/2 17:21
 **/
@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {
    @Autowired
    CommentService commentService;
    /**
     * @Author WJ
     * @Description 保存评价
     * @Date 2023/4/2 21:26
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @Param [comment]
     **/
    @PostMapping("/saveComment")
    @ApiOperation(value ="评论新增方法")
    @OptLog(modular = "评论管理", type = CommonConstants.ADD, describe = "新增评论")
    public CommonResult saveComment(@RequestBody Comment comment){
        int i = commentService.saveComment(comment);
        if (i > 0){
            return CommonResult.ok(i);
        }else {
            return CommonResult.error(100003,"保存失败");
        }
    }
    /**
     * @Author WJ
     * @Description 保存回复评论
     * @Date 2023/4/5 7:26
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @Param [comment]
     **/
    @PostMapping("/saveReply")
    @ApiOperation(value ="回复评论方法")
    @OptLog(modular = "评论管理", type = CommonConstants.ADD, describe = "回复评论")
    public CommonResult saveReply(@RequestBody Comment comment){
        int i = commentService.saveReply(comment);
        if (i > 0){
            return CommonResult.ok(i);
        }else {
            return CommonResult.error(100003,"保存失败");
        }
    }
    /**
     * @Author WJ
     * @Description 查询全部评论通过商品id
     * @Date 2023/4/3 17:42
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @Param [pageRequest,divId]
     **/
    @GetMapping("/getAllCommentByDivId")
    @ApiOperation(value ="查询评论方法")
    @OptLog(modular = "评论管理", type = CommonConstants.SELECT, describe = "查询评论")
    public CommonResult getAllCommentByDivId(PageRequest pageRequest, Integer divId){
        PageResult result = commentService.getAllCommentByDivId(pageRequest,divId);
        if (result != null){
            return CommonResult.ok(result);
        }else {
            return CommonResult.error(100023, "没有此查询结果");
        }
    }
    /**
     * @Author WJ
     * @Description 删除评论及方法
     * @Date 2023/4/6 18:59
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @Param [id]
     **/
    @GetMapping("/deleteCommentById")
    @ApiOperation(value ="删除评论及回复方法")
    @OptLog(modular = "评论管理", type = CommonConstants.DELETE, describe = "删除评论")
    public CommonResult deleteCommentById(int id){
        return CommonResult.ok(commentService.deleteCommentById(id));
    }
}
